package pe.edu.upc.jtigers.svpn.web.controller;

import java.util.Collection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import pe.edu.upc.jtigers.svpn.domain.model.Compra;
import pe.edu.upc.jtigers.svpn.domain.model.PlanNegocio;
import pe.edu.upc.jtigers.svpn.domain.service.CompraService;
import pe.edu.upc.jtigers.svpn.web.common.BaseController;
import pe.edu.upc.jtigers.svpn.web.common.RequestMappingConstant;
import pe.edu.upc.jtigers.svpn.web.common.ResponseViewConstant;

@Controller
public class CompraController extends BaseController {
	@Autowired
	private CompraService compraService;

	@RequestMapping(RequestMappingConstant.LISTA_COMPRA)
	public ModelAndView listaCompra() {
		String vista = ResponseViewConstant.VIEW_LISTA_COMPRA;
		Collection<PlanNegocio> colCompra = compraService.listarCompras(this.getUsuarioLogueado().getIdUsuario());
		return new ModelAndView(vista, "planes", colCompra);
	}

	@RequestMapping(RequestMappingConstant.REGISTRAR_COMPRA)
	public @ResponseBody
	Compra registraCompra(@RequestBody Compra compra) {
		compra = compraService.registrarCompra(compra);
		return compra;
	}

	@RequestMapping(RequestMappingConstant.VALIDA_COMPRA)
	public @ResponseBody
	Compra validaCompra(@RequestBody Compra compra) {
		compra = compraService.validaCompra(compra);
		return compra;
	}

	@RequestMapping(RequestMappingConstant.COMPLETA_COMPRA)
	public ModelAndView completaCompra(@RequestParam("id") int id) {
		Compra compra = compraService.completaCompra(id);
		return new ModelAndView(ResponseViewConstant.VIEW_CONPLETA_COMPRA, "compra", compra);
	}
}
